package com.ggs.rg.ybjb.adminmodule.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ggs.rg.ybjb.adminmodule.exception.OrderException;
import com.ggs.rg.ybjb.adminmodule.mapper.MaterMapper;
import com.ggs.rg.ybjb.adminmodule.service.MaterService;
import com.ggs.rg.ybjb.adminmodule.service.TGoodsOrderService;
import com.ggs.rg.ybjb.adminmodule.service.TOrderService;
import com.ggs.rg.ybjb.adminmodule.util.RedisUtil;
import com.ggs.rg.ybjb.pojomodule.pojo.Mater;
import com.ggs.rg.ybjb.pojomodule.pojo.TGoodsOrder;
import com.ggs.rg.ybjb.pojomodule.pojo.TOrder;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import com.ggs.rg.ybjb.pojomodule.util.constants.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


/**
 * @author cxw
 * @since 2021-11-02
 */
@RestController
@RequestMapping("/orderModule")
public class TOrderController {

    @Autowired
    TOrderService tOrderService;

    @Autowired
    TGoodsOrderService tGoodsOrderService;

    @Autowired
    MaterService materService;

    @Autowired
    private RedisUtil redisUtil;            //自己写的操作redis的工具类,其实就是封装了redisTemplate的操作

    //查询所有订单
    //完整的信息包括:订单基本信息,收货地址,购买的商品信息(可能有多个)
    @GetMapping("/getOrders")
    public JSONObject getOrders(@RequestParam(value = "pagesNumber") Integer pagesNumber,@RequestParam("status") Integer status){
        JSONObject result;
        //传-1默认是查所有
        if (status == -1){
            //list用来存放获取到的所有订单表数据和订单地址
            result = tOrderService.selectOrders(pagesNumber,null);
        }else {
            result = tOrderService.selectOrders(pagesNumber,status);
        }
        return CommonUtil.successJson(result);
    }


    //搜索,根据oid获取订单
    @GetMapping("/getOrderById")
    public JSONObject getOrderById(@RequestParam("oid") Long oid){
        JSONObject result = tOrderService.selectTOrderById(oid);
        return CommonUtil.successJson(result);
    }


    //根据oid获取订单详情
    @GetMapping("/getOrderDetailById")
    public JSONObject getOrderDetailsById(@RequestParam("oid")Long oid){
        TOrder order = tOrderService.getById(oid);
        if (StringTools.isNullOrEmpty(order))
            return CommonUtil.sqlErrorJson("oid为:" + oid + "的订单不存在在");
        JSONObject result = null ;
        Integer status = order.getStatus();
        if (status == 1){
            result = tOrderService.selectTOrderDetailById(oid);
        }else {
            result = tOrderService.selectUnpaidTOrderDetailById(oid);
        }
        return CommonUtil.successJson(result);
    }

    //获取总订单数
    @GetMapping("/getOrderCount")
    public JSONObject getOrderCount(){
        return CommonUtil.successJson(tOrderService.count());
    }

    //获取总交易额
    @GetMapping("/transactionAmount")
    public JSONObject transactionAmount(){
        //记录总金额
        Double count = 0d ;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",1);
        List<TOrder> list = tOrderService.list(queryWrapper);
        for (TOrder order:list){
            count += order.getPrice();
        }
        return CommonUtil.successJson(count);
    }

    //首页数据  获取前七天数据（订单数量、订单总金额）
    @GetMapping("/getOrderData")
    public JSONObject getAllOrder(){
        JSONObject result = new JSONObject();
        //设置一个double类型的变量，累计计算商品订单金额
        Double WEEKMAX = 0.00;
        Double MONTHMAX = 0.00;
        //获取当天时间的00-00-00
        Calendar calendar = Calendar.getInstance();
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        Calendar calendar5 = Calendar.getInstance();

        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        System.out.println("本周周一是" + calendar.get(Calendar.DAY_OF_WEEK));
        //根据时间筛选当天00-00-00前七天的数据
        calendar1.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        calendar1.add(Calendar.DATE,-8);
        //获取前一个月的第一天和最后一天  一定要初始化当天的时、分、秒
        calendar2.add(Calendar.MONTH,-1);
        calendar2.set(Calendar.DAY_OF_MONTH,calendar2.getMinimum(Calendar.DAY_OF_MONTH));
        calendar2.set(Calendar.HOUR_OF_DAY,0);
        calendar2.set(Calendar.MINUTE,0);
        calendar2.set(Calendar.SECOND,0);
        calendar5.add(Calendar.MONTH,0);
        calendar5.set(Calendar.DAY_OF_MONTH,calendar5.getMinimum(Calendar.DAY_OF_MONTH));
        calendar5.set(Calendar.HOUR_OF_DAY,0);
        calendar5.set(Calendar.MINUTE,0);
        calendar5.set(Calendar.SECOND,0);
        //条件判断查询一周前的所有商品
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        QueryWrapper<TOrder> tOrderQueryWrapper = queryWrapper.ge("create_time", calendar1).lt("create_time", calendar).eq("status", 1);
        List<TOrder> list = tOrderService.list(tOrderQueryWrapper);
        for (TOrder to : list){
            double price = to.getPrice();
            WEEKMAX = WEEKMAX + price;
        }
        //保留两位小数点
        BigDecimal bd = new BigDecimal(WEEKMAX);
        double value = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        //获取订单个数
        int size = list.size();
        Map<String,Object> week = new TreeMap<>();
        week.put("OrderNumber",size);
        week.put("OrderMoney",value);
        result.put("week",week);
        //条件判断查询一个月前的所有商品
        QueryWrapper<TOrder> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<TOrder> tOrderQueryWrapper1 = queryWrapper1.ge("create_time", calendar2).lt("create_time", calendar5).eq("status", 1).select("price");
        List<TOrder> list1 = tOrderService.list(tOrderQueryWrapper1);
        for (TOrder to : list1){
            double price = to.getPrice();
            MONTHMAX = MONTHMAX + price;
        }
        //保留两位小数点
        BigDecimal bd1 = new BigDecimal(MONTHMAX);
        double value1 = bd1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        //获取订单数量
        int size1 = list1.size();
        //获取当前月份前一个月
        int MONTH = calendar1.get(Calendar.MONTH);
        //
        Map<String,Object> month = new TreeMap<>();
        month.put("OrderMonth",MONTH);
        month.put("OrderNumber",size1);
        month.put("OrderMoney",value1);

        result.put("month",month);
        //前一周的所有订单金额，一天一天输出
        Map<String,Map<String,Object>> sevenDay = new TreeMap<>();
        for (int i = 1; i <= 7; i++) {
            //初始化
            Calendar calendar3 = Calendar.getInstance();
            Calendar calendar4 = Calendar.getInstance();
            //设置前一周以一天为区间取时间段
            calendar3.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
            calendar3.set(Calendar.HOUR_OF_DAY,0);
            calendar3.set(Calendar.MINUTE,0);
            calendar3.set(Calendar.SECOND,0);
            calendar4.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
            calendar4.set(Calendar.HOUR_OF_DAY,0);
            calendar4.set(Calendar.MINUTE,0);
            calendar4.set(Calendar.SECOND,0);
            Double DAYMAX = 0.00;
            calendar3.add(Calendar.DATE,(-8+i));
            calendar4.add(Calendar.DATE,(-7+i));
            QueryWrapper<TOrder> queryWrapper2 = new QueryWrapper<>();
            QueryWrapper<TOrder> tOrderQueryWrapper2 = queryWrapper2.ge("create_time", calendar3).lt("create_time", calendar4).eq("status", 1).select("price");
            List<TOrder> list2 = tOrderService.list(tOrderQueryWrapper2);
            for (TOrder to : list2){
                double price = to.getPrice();
                DAYMAX = DAYMAX + price;
            }
            //保留两位小数点
            BigDecimal bd2 = new BigDecimal(DAYMAX);
            double value2 = bd2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            //获取订单数量
            int size2 = list2.size();
            //获取当前日期
            final int year = calendar3.get(Calendar.YEAR);
            int MONTH1 = calendar3.get(Calendar.MONTH) + 1;
            int DAY = calendar3.get(Calendar.DAY_OF_MONTH);
            //
            Map<String,Object> day = new TreeMap<>();
            day.put("Data",year + "-" + MONTH1 + "-" + DAY);
            day.put("DayNumber",size2);
            day.put("DayMoney",value2);
//            result.put("DAY" + i,day);
            sevenDay.put("DAY" + i,day);
            //
        }
        result.put("SevenDays",sevenDay);
        return result;
    }
}

